import Vue from 'vue'
import Router from 'vue-router'
// 引入Login组件
import Login from './components/Login.vue'
import Home from './components/Home.vue'
import Welcome from './components/Welcome.vue'
import User from './components/user/User.vue'
Vue.use(Router)

const router = new Router({
  routes: [{
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/home',
      component: Home,
      redirect:'/welcome',
      children:[
        { path:'/welcome',component:Welcome },
        { path:'/users',component:User }
      ]
    }
  ]
})

router.beforeEach((to,from,next)=>{
    // to 到哪去
    // from 从哪来
    // next 继续

    if(to.path == "/login") return next()

    // 如果不是去login那么我们就要判断是否有token值
    const tokenStr = window.sessionStorage.getItem('token')

    if(!tokenStr){ // 如果为false 则 强制跳转到login
      return next('/login')
    }

    next()

})


export default router